/*
 * Copyright (c) 2010, Leon Dang f3it.com
 *           (c) 2010, Andrew Korolev redfox14 at gmail dot com
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 *   * Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *   * Neither the name of the Leon Dang & FCUBEDIT nor the names of its
 *     contributors may be used to endorse or promote products derived from
 *     this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
 
#ifndef LOGDB_H
#define LOGDB_H

#include <QObject>
#include <QSqlDatabase>
#include <QDateTime>
#include <QList>

#include "settings.h"

class DBRecord: public QObject
{
	Q_OBJECT

	public:
		DBRecord( QObject * parent = 0 ): QObject( parent) {;}
		QTime startTime() const { return m_startTime.time(); }
		QDate startDate() const { return m_startTime.date(); }
		QDateTime startDateTime() const { return m_startTime; }
		QTime endTime() const { return m_endTime.time(); }
		QDate endDate() const { return m_endTime.date(); }
		QDateTime endDateTime() const { return m_endTime; }
		QString callDuration() const
		{
			QString str;
			QTime t;
			t.setHMS(0, 0, 0);
			if ( ! m_endTime.toTime_t() == 0 )
			{
				uint delta = m_endTime.toTime_t() - m_startTime.toTime_t();
				uint hours = delta / 3600;
				delta = delta % 3600;
				uint mins = delta / 60;
				uint secs = delta % 60;
				t.setHMS( hours, mins, secs );
			}
			QString s1;
			if ( t.hour() > 0 )
			{
				s1.setNum( t.hour() );
				str.append( s1 );
				str.append( tr( "h " ) );
			}
			if ( t.minute() > 0 )
			{
				s1.setNum( t.minute() );
				str.append( s1 );
				str.append( tr( "m " ) );
			}
				s1.setNum( t.second() );
				str.append( s1 );
				str.append( tr( "s" ) );
			return str;
		}
		EVENT_TYPE recordType() const { return m_recordType; }
		EVENT_DIRECTION direction() const { return m_direction; }
		QString name() const { return m_name; }
		QString phoneNumber() const { return m_phoneNumber; }
		
		void setStartTime( uint t ) { m_startTime.setTime_t( t ); }
		void setEndTime( uint t ) { m_endTime.setTime_t( t ); }
		void setRecordType( EVENT_TYPE type ) { m_recordType = type; }
		void setDirection( EVENT_DIRECTION s ) { m_direction = s; }
		void setName( QString & n ) { m_name = n; }
		void setPhoneNumber( QString & p ) { m_phoneNumber = p; }
		
		virtual CALL_TYPE callType() const { return CT_NONE; }
		virtual void setCallType( CALL_TYPE c ) { Q_UNUSED( c ); }
		virtual bool isRead() const { return false; }
		virtual QString text() const { return QString( "" ); }
		virtual void setRead( bool r ) { Q_UNUSED( r ); }
		virtual void setText( QString & t ) { Q_UNUSED( t ); }
		
	protected:
		EVENT_TYPE m_recordType;
		EVENT_DIRECTION m_direction;
		QString m_name;
		QString m_phoneNumber;
		QDateTime m_startTime;
		QDateTime m_endTime;
};

class CallRecord : public DBRecord
{
	Q_OBJECT
	
	public:
		CallRecord( QObject * parent = 0 ): DBRecord( parent ) {;}
		CALL_TYPE callType() const { return m_callType; }
		void setCallType( CALL_TYPE c ) { m_callType = c; }

	private:
		CALL_TYPE m_callType;
};

class SMSRecord: public DBRecord
{
	Q_OBJECT
	
	public:
		SMSRecord( QObject * parent = 0 ): DBRecord( parent ) {;}
		bool isRead() const { return m_isRead; }
		QString text() const { return m_text; }
		void setRead( bool r ) { m_isRead = r; }
		void setText( QString & t ) { m_text = t; }
	
	private:
		bool m_isRead;
		QString m_text; 
};

class LogDB: public QObject
{
Q_OBJECT

public:
	LogDB( QObject * parent = 0 );
	~LogDB();
	bool connectDB();
	void disconnectDB();
	void restart();
	const DBRecord *getNextRecord();
	const DBRecord *getRecordAt(int i) { return ((i >= 0 && i < m_dbRecs.size()) ? m_dbRecs.at(i) : NULL); }
	void setFilterOptions( OptionsClass::FilterOptions f );
	void setEventFilter( OptionsClass::FilterOptions f );
	void setDirectionFilter( OptionsClass::FilterOptions f );
	int maxRecords() const { return m_maxRecords; }
	OptionsClass::FilterOptions filterOptions() const { return m_filterOptions; }
	QList<DBRecord *> dbRecs() const { return m_dbRecs; }

public slots:
	void setMaxRecords( int m );
	bool setupQuery();

private:
	DBRecord *getNextDBRecord();

	QSqlDatabase m_db;
	QSqlQuery * m_dbq;
	OptionsClass::FilterOptions m_filterOptions;
	int m_maxRecords;
	QList<DBRecord *> m_dbRecs;

	int m_at;

signals:
	void variableChanged();
};

#endif // LOGDB_H
